#!/bin/bash
#功能函数
funcs=`which functions.sh`
if [ "$funcs" != "" ] ;then
    . $funcs
else
    funcs=`dirname $0`/functions.sh
    echo "$PWD"
    echo "funcs=$funcs"
    if [ "$funcs" != "" ] ;then
	. $funcs
    else
	exit
    fi
fi

IMG_SUFFIX=img
def_mount_point=~/mnt/system
def_system_size=512
def_ext4fs_flags="-s -l ${def_system_size}M -a"
def_yaffs2_flags=" "
if test -d frameworks; then
  HOST_OUT_EXECUTABLES=out/host/linux-x86/bin/
  MKBOOTIMG=$HOST_OUT_EXECUTABLES/mkbootimg
  MKEXTUSERIMG=$HOST_OUT_EXECUTABLES/mkuserimg.sh
  MKEXT2BOOTIMG=external/genext2fs/mkbootimg_ext2.sh
  SIM2IMG=$HOST_OUT_EXECUTABLES/simg2img
  MAKE_EXT4FS=$HOST_OUT_EXECUTABLES/make_ext4fs
else
  MKBOOTIMG=`which mkbootimg`
  MKEXTUSERIMG=`which mkuserimg.sh`
  MKEXT2BOOTIMG=`which mkbootimg_ext2.sh`
  SIM2IMG=$HOST_OUT_EXECUTABLES/simg2img
  MAKE_EXT4FS=`which make_ext4fs`
fi

if test -f ./makeMtk.ini ; then
  target_product=`cat  ./makeMtk.ini |grep project |awk '{print $3}'`
else
  target_product=xxxx
fi


function find_files_rm()
{
  local files_list_rm=
  E_OPTIONS=`build/tools/findleaves.py  --prune=.repo --prune=.git packages/ vendor/bbk/source/  vendor/bbk/common/googleapps Android.mk |xargs grep -E "^\s*LOCAL_PACKAGE_NAME|^\s*LOCAL_MODULE" -w |awk -F:= '{print $2}' |sort |uniq | tr -d ' '|tr '\n' '|' | sed "s/|$//"`
  files_list_rm=`find.sh -d $my_system |grep -E "$E_OPTIONS" | awk -F "system/" '{print $2}'`
  echo "$files_list_rm"
}

function merge_apps()
{
      local release_system=$1
      local my_system=$2
      if [ -z "$release_system" ] || [ -z "$my_system" ]; then
        echoTextRed "合并存在问题!"
	return 1
      fi
      #获取需要拷贝的文件列表
      if test ! -f $release_system/etc/all-appfiles; then
	echo "无法获取应用列表文件 $release_system/etc/all-appfiles ,请尝试源码根目录执行此脚本!!!" >&2
	files_list_cp=`find_files_rm`
      else
	files_list_cp=`cat $release_system/etc/all-appfiles`
      fi
      #获取需要删除的文件列表
      if test ! -f $my_system/etc/all-appfiles; then
	echo "无法获取应用列表文件 $my_system/etc/all-appfiles ,请尝试源码根目录执行此脚本!!!"
	files_list_rm=`find_files_rm`
      else
	files_list_rm=`cat $my_system/etc/all-appfiles`
      fi
      #开始合并文件系统操作
      echoTextBlue "开始合并文件系统......"
      if [ -n "$files_list_rm" ]; then
        cd ${my_system}; rm -rf $files_list_rm;cd -
      else
         echo "合并存在问题,无法获取应用列表文件"
         return 1
      fi
      #开始合并文件
      echoTextBlue "拷贝应用到 $my_system......"
      for f in $files_list_cp
      do
        cp -rf $release_system/$f   ${my_system}/$f
      done
      return 0
}

function mkimg-yaffs-merge-release-apps()
{
  echoTextBlue "暂时未能处理 yaffs 文件系统!!"
}


function useage()
{
echoTextGreen "用法:  `basename $1` arg1  [arg2]
arg1   配置组发布的系统分区镜像文件(system.img),参数1不能为空
arg2   参数2为可选项,不为空的情况下通常:
       1. 自己编译生成的 out/target/product/$target_product/system     目录
       2. 自己编译生成的 out/target/product/$target_product/system.img 镜像
示例:
1. mkimg-merge-release-apps.sh /path/to/release/system.img
2. mkimg-merge-release-apps.sh /path/to/release/system.img /path/to/android/src/out/target/product/$target_product/system
3. mkimg-merge-release-apps.sh /path/to/release/system.img /path/to/android/src/out/target/product/$target_product/system.img
* 方法1 基于发布版本 /path/to/release/system.img,仅增加自己的上层应用,脚本过程会提示用户添加自己的应用到对应目录,然后生成一个新的镜像
* 方法2 方法3 功能相同,使用自己编译的 system 分区,合并发布版本的其他上层应用, 参数2 可以为生成的 system 分区目录,也可以为生成的 system 分区镜像
* 因为在合并过程会先删除你编译结果中的所有相关上层应用,如果要使用自己编译的某个上层应用,那么类似方法一,在生成镜像的过程中会提示你单独添加
* 参数2 的分区或者镜像都会单独复制一份进行操作,所有无需担心被修改
"
}

function get_ext4fs_flags()
{
  local systemimage_part_size=
  local emmc_size_mk=mediatek/config/$target_product/configs/EMMC_partition_size.mk
  if test -f $emmc_size_mk; then
    systemimage_part_size=`cat $emmc_size_mk | grep BOARD_SYSTEMIMAGE_PARTITION_SIZE | awk -F":=" '{print $2}'`
    if [ -z "$systemimage_part_size" ]; then
      ext4fs_flags=`question  "无法获取 BOARD_SYSTEMIMAGE_PARTITION_SIZE 值, make_ext4fs 使用默认值"  2 "$def_ext4fs_flags"`
    else
      ext4fs_flags="-s -l ${systemimage_part_size} -a"
    fi
  else
    ext4fs_flags=`question  "无法获取 BOARD_SYSTEMIMAGE_PARTITION_SIZE 值, make_ext4fs 使用默认值"  2 "$def_ext4fs_flags"`
  fi
}


#获取参数
system_img=$1
my_system=$2
if [ -n "$system_img" ]; then
  if `sh_strstr  $system_img ext4`; then
    IMG_SUFFIX=img.ext4
  fi
fi


#yaffs文件系统的处理
if [ -n "$system_img" ]; then
  result=`file $system_img |grep VMS |grep Alpha`
  if [ -n "$result" ]; then
      mkimg-yaffs-merge-release-apps
      exit
  fi
fi

#echo "IMG_SUFFIX=$IMG_SUFFIX"
get_ext4fs_flags
#挂载system.img
if [ -z "$system_img" ]; then
  useage $0
else
  if [ -z "$my_system" ]; then   
    #卸载所有已经挂载的ext4镜像
    sh_umount_all_ext4_img ${system_img}_new ${system_img}_new_format
    cp ${system_img} ${system_img}_new -rfv 
    mount_point=`dirname $def_mount_point`/system_new
    mount_point_n=$mount_point
    echoTextBlue "挂载ext4分区到 $mount_point ......"
    if ! `sh_mount_ext4_img ${system_img}_new $mount_point` ; then
      echoTextRed "${system_img}_new 挂载失败!"
      exit
    fi
    echoTextBlue "切换 $mount_point 权限 ......"
    sh_chown_ext4_img_parttion $mount_point
    result=`question "将会重新生成新的 system.img ,如果需要手动在编译文件系统内容(如:单独增加应用,修改专项版本号), 请在 $mount_point 目录进行,完成后再选择 yes 继续, no 退出!" 1 yes`
    if [ "$result" = "yes" ]; then
      echoTextBlue "$MAKE_EXT4FS $ext4fs_flags system  `dirname ${system_img}`/system_new.$IMG_SUFFIX $mount_point"
      $MAKE_EXT4FS $ext4fs_flags system  `dirname ${system_img}`/system_new.$IMG_SUFFIX $mount_point
      echoTextGreen "新的镜像文件: `dirname ${system_img}`/system_new.$IMG_SUFFIX !"
    else
      echoTextRed "放弃打包镜像操作!"
      echoTextGreen "手动输入以下命令重新打包:"
      echoTextBlue "$MAKE_EXT4FS $ext4fs_flags system  `dirname ${system_img}`/system_new.$IMG_SUFFIX $mount_point"
    fi
    #卸载所有已经挂载的ext4镜像
    sh_umount_all_ext4_img ${system_img}_new ${system_img}_new_format
    rm $mount_point_n -rf
  else
    my_system=`sh_delete_last_slash $my_system`
    #如果参数2为目录
    if test -d $my_system; then
      echoTextBlue "处理中,请稍后......"
      #卸载所有已经挂载的ext4镜像
      sh_umount_all_ext4_img ${system_img}_format ${my_system}_new
      mount_point=`dirname $def_mount_point`/system_release
      mount_point_r=$mount_point
      echoTextBlue "挂载ext4分区到 $mount_point ......"
      if ! `sh_mount_ext4_img $system_img $mount_point` ; then
	  echoTextRed "$system_img 挂载失败!"
          exit
      fi
      rm -rf ${my_system}_new
      echo "cp ${my_system} ${my_system}_new -rf"
      cp ${my_system} ${my_system}_new -rf
      merge_apps $mount_point_r ${my_system}_new
      result=`question "将会重新生成新的 system.img ,如需要手动编译文件系统内容(eg:单独增加应用,修改专项版本号), 请在 $mount_point 目录进行,完成后再选择 yes 继续, no 退出!" 1 yes`
      if [ "$result" = "yes" ]; then
	echoTextBlue "$MAKE_EXT4FS $ext4fs_flags system  `dirname ${my_system}_new`/system_new.$IMG_SUFFIX ${my_system}_new"
	$MAKE_EXT4FS $ext4fs_flags system  `dirname ${my_system}_new`/system_new.$IMG_SUFFIX  ${my_system}_new
	echoTextGreen "新的镜像文件: `dirname ${my_system}_new`/system_new.$IMG_SUFFIX !"
      else
	echoTextRed "放弃打包镜像操作!"
      fi
      #卸载所有已经挂载的ext4镜像
      sh_umount_all_ext4_img ${system_img}_format ${my_system}_new
      rm $mount_point_r  -rf
    else #如果参数2为镜像
      #卸载所有已经挂载的ext4镜像
      sh_umount_all_ext4_img  ${system_img}_format ${my_system}_new ${my_system}_new_format
      cp ${my_system} ${my_system}_new -rfv 
      mount_point=`dirname $def_mount_point`/system_release
      mount_point_r=$mount_point
      echoTextBlue "挂载ext4分区到 $mount_point ......"
      if ! `sh_mount_ext4_img $system_img $mount_point` ; then
	  echoTextRed "$system_img 挂载失败!"
          exit
      fi
      #mount_point=`dirname $def_mount_point`/system_you
      #mount_point_y=$mount_point
      #echoTextBlue "挂载ext4分区到 $mount_point ......"
      #if ! `sh_mount_ext4_img ${my_system} $mount_point` ; then
	  #echoTextRed "${my_system} 挂载失败!"
          #exit
      #fi
      mount_point=`dirname $def_mount_point`/system_new
      mount_point_n=$mount_point
      echoTextBlue "挂载ext4分区到 $mount_point ......"
      if ! `sh_mount_ext4_img ${my_system}_new $mount_point` ; then
          echoTextRed "${my_system}_new 挂载失败!"
	  exit
      fi
      echoTextBlue "切换 $mount_point 权限 ......"
      sh_chown_ext4_img_parttion $mount_point
      merge_apps $mount_point_r $mount_point_n

      result=`question "将会重新生成新的 system.img ,如需要手动编译文件系统内容(eg:单独增加应用,修改专项版本号), 请在 $mount_point 目录进行,完成后再选择 yes 继续, no 退出!" 1 yes`
      if [ "$result" = "yes" ]; then
	echoTextBlue "$MAKE_EXT4FS $ext4fs_flags system  `dirname ${my_system}`/system_new.$IMG_SUFFIX $mount_point"
	$MAKE_EXT4FS $ext4fs_flags system  `dirname ${my_system}`/system_new.$IMG_SUFFIX $mount_point
	echoTextGreen "新的镜像文件: `dirname ${my_system}`/system_new.$IMG_SUFFIX!"
      else
	echoTextRed "放弃打包镜像操作!"
      fi
      #卸载所有已经挂载的ext4镜像
      sh_umount_all_ext4_img  ${system_img}_format ${my_system}_new ${my_system}_new_format
      rm $mount_point_r $mount_point_y $mount_point_n -rf
    fi
  fi
fi
